<?php 
class PublicSurvey extends Module{
	function PublicSurvey($row)
	{
		Module::Module($row);
		
		$this->add_form(new PublicSurveyForm());
	}
}

class PublicSurveyForm extends Form
{
	function PublicSurveyForm()
	{
		Form::Form('PublicSurveyForm');

		if(Url::get('cmd')=='add_poll_to_block' && (int)Url::get('block_id') == Module::$block_id)
		{
			$id_survey = (int)Url::get('poll_id');
			
			if($id_survey)
			{
				$survey = DB::select("public_survey","id=$id_survey");
				
				if($survey)
				{
					$survey_config = @CGlobal::$configs['survey_config']['conf_val'];
			
					if($survey_config != '')
					{
						$survey_config = unserialize(stripslashes($survey_config));
					}
					else 
					{
						$survey_config = array();
					}
					
					$survey_config[Module::$block_id] = $id_survey;
					
					if(isset(CGlobal::$configs['survey_config']))
						DB::update('configs',array("conf_val"=>addslashes(serialize($survey_config))),"conf_key='survey_config'");
					else
						DB::insert('configs',array("conf_key"=>'survey_config',"conf_val"=>addslashes(serialize($survey_config))));
					
					AZLib::get_config(0,1);
				}
				
				Url::redirect_current();
			}
		}
		
		if(Url::get('cmd')=='remove_survey' && (int)Url::get('block_id') == Module::$block_id)
		{
			
			$survey_config = @CGlobal::$configs['survey_config']['conf_val'];
			
			if($survey_config != '')
			{
				$survey_config = unserialize(stripslashes($survey_config));
			}
			else 
			{
				$survey_config = array();
			}
			
			if(isset($survey_config[Module::$block_id]))
			{
				unset($survey_config[Module::$block_id]);
			}
			
			if(isset(CGlobal::$configs['survey_config']))
				DB::update('configs',array("conf_val"=>addslashes(serialize($survey_config))),"conf_key='survey_config'");
			else
				DB::insert('configs',array("conf_key"=>'survey_config',"conf_val"=>addslashes(serialize($survey_config))));
			
			AZLib::get_config(0,1);
			
			Url::redirect_current();
			/*if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']){
				$referer = $_SERVER['HTTP_REFERER'];
			}
			else{
				$referer = $_SERVER['REQUEST_URI'];
			}
			
			Url::redirect_url($referer);*/
		}
	}
	
	function draw()
	{
		global $display;
		
		$survey_config = @CGlobal::$configs['survey_config']['conf_val'];

		if($survey_config != '')
		{
			$survey_config = unserialize(stripslashes($survey_config));
		}
		else 
		{
			$survey_config = array();
		}

		$id_survey	=	@(int)$survey_config[Module::$block_id];
		$admin = '';
		
		if (User::is_admin())
		{
			$admin = '<a href="'.Url::build('admin_survey',array('href'=>urlencode(Url::build_all()),'block_id'=>Module::$block_id)).'"><img src="style/images/nav_right_arrow.gif"  border="0" title="Chọn thăm dò ý kiến" /></a>';
			$display->add('is_admin',1);
		}
		
		$public_survey	=	DB::select("public_survey","id=$id_survey");

		if($public_survey)
		{
			if ($public_survey['view'] == 1)
			{
				$display->add('show_survey',1);
				
				$display->add('title',		stripslashes($public_survey['title']));
				$display->add('question',	stripslashes($public_survey['question']));
				
				$display->add('id',$public_survey['id']);
						
				$opts = array();
				
				$re = DB::query('SELECT * FROM public_survey_opinion WHERE `id_survey`='.$public_survey['id']);
				
				$i = 0;
				
				if($re)
				{
					while ($opt = mysql_fetch_assoc($re)) 
					{
						$i++;
						
						if ($public_survey['type'] == 1)
						{
							$opt['control'] = '<input name="survey_id[]" type="radio" value="'.$opt['id'].'" id="survey_id_'.$opt['id'].'">';
						}
						else
						{
							$opt['control'] = '<input name="survey_id[]" type="checkbox" value="'.$opt['id'].'" id="survey_id_'.$opt['id'].'">';
						}
						
						$opts[$opt['id']] = $opt;
					}
				}
				
				$display->add('opts', $opts);
				
				
				$url_detail=Url::build('view_survey',array('id'=>$public_survey['id']));
				$display->add('onclick_rate',Url::open_popup($url_detail."&ids='+survey_list('survey_id[]')+'",600,100+50*$i,false,false,false,false,false,1,false,false,false));
						
				$display->add('onclick_view',Url::open_popup($url_detail,600,100+50*$i,false,false,false,false,false,1,false,false,false));
			}
			
			$display->add('edit',	AZLib::button(		Url::build('admin_survey',array('cmd'=>'edit', 'id_survey'=>$public_survey['id'])),												'style/images/edit.gif',"Sửa Thăm dò ý kiến"));
			$display->add('delete',	AZLib::buttonDel(	Url::build_current(array('cmd'=>'remove_survey','id_survey'=>$public_survey['id'],'block_id'=>Module::$block_id)),	'style/images/delete.gif',"Bỏ chọn dò ý kiến"));
		}
		
		$display->add('admin',$admin);
		
		$display->output('PublicSurvey');
	}
}
?>